<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/4/19
 * Time: 14:42
 */

class News extends M_Controller
{
    public function index(){
        list($data, $total, $param)	= $this->_limit_page(); // 数据库中分页查询
        $id = (int)$param['id'];

//        $list = $this->db->where("1=1")->order_by("id desc")->get("news")->result_array();
        $this->template->assign(
            array(
                'total' => $total,
                'param' => $param,
//                'tushu'=>$this->db->where("type",1)->order_by("id desc")->get("news")->result_array(),
//                'hxtt'=>$this->db->where("type",2)->order_by("id desc")->get("news")->result_array(),
//                'jydt'=>$this->db->where("type",3)->order_by("id desc")->get("news")->result_array(),
                "list"=>$data,
                "hotspots"=>$this->db->where("hotspot",1)->order_by("id desc")->get("news")->result_array(),
                'pages'	=> $this->get_pagination(dr_url('admin/apply/main', array(
                    'uid' => $id,
                    'total' => $total,
                    'search' => IS_POST ? 1 : $this->input->get('search')
                )), $total)
            )
        );
        $this->template->display("../gw/news.html");
    }

    private function  _limit_page(){
        $page = max((int)$this->input->get_post('page'),1);
        $_total = (int)$this->input->get_post('total');

        if(!$_total){
            $_total = $this->db->count_all_results('news');
            if(!$_total){
                return array(array(), 0);
            }
        }
        $select = $this->db;
        $result = $this->db
            ->limit(SITE_ADMIN_PAGESIZE, SITE_ADMIN_PAGESIZE * ($page - 1))
            ->order_by('id desc')
            ->get('news')
            ->result_array();
//        return array($result,$_total, $_param);
        return array($result,$_total);
    }


    public function detail(){
        $nid = $this->input->get_post('nid');
        if(!$nid){
            $this->admin_msg('参数不能为空');
        }
        //
        $data = $this->db->where('id',$nid)->get('news')->row_array();
        if(empty($data)){
            $this->admin_msg('数据不存在或者已经删除');
        }
       //热点新闻
        $hotpot = $this->db->where('hotspot',1)->get('news')->result_array();
        $this->template->assign(array(
            'data'=>$data,
            'hotspot'=>$hotpot

        ));
        $this->template->display('../gw/news_detail.html');
    }
}